package com.pda.yuanjing.pages.syyk;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.JieDianBeanIn;
import com.pda.yuanjing.beanout.NoBillBeanOut;
import com.pda.yuanjing.thread.CodeBeans;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ScanfModelImpl extends BaseModel implements ScanfModel {
    private String TAG = "InScanfModelImpl";

    public ScanfModelImpl(Context context) {
        super(context);
    }

    @Override
    public void scanfCodenobill(String  billCode,  String codes,String sid,CodeBeans codeBeans, CallBack callBack)
    {
        NoBillBeanOut jdBeanOut = new NoBillBeanOut();
        jdBeanOut.setErrcode(-1);
        jdBeanOut.setErrmsg(SetConfig.getConnectError());
        try {
            JieDianBeanIn jieDianBeanIn = new JieDianBeanIn();
            jieDianBeanIn.setAction("SY_UpdateCode_ZiYou");
            jieDianBeanIn.setCid(SharedPreferencesManger.getCid());
            jieDianBeanIn.setUserID(SharedPreferencesManger.getUid());
            jieDianBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            jieDianBeanIn.setBillCode(billCode);
            jieDianBeanIn.setStoreIDIn(sid);
            jieDianBeanIn.setCode(codes);
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(jieDianBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                jdBeanOut = gson.fromJson(StringProcess.responsetoString(response.body()), NoBillBeanOut.class);
                jdBeanOut.getData().setCode(jdBeanOut.getErrcode());
                jdBeanOut.getData().setStr(jdBeanOut.getErrmsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            callBack.onshowSuccessresNoBill(jdBeanOut.getData());
        }
    }
}

